﻿<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Create a HTML to PDF converter object with default settings</span>
    HtmlToPdfConverter htmlToPdfConverter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> HtmlToPdfConverter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed</span>
    <span style='color:#696969; '>// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#008c00; '>2</span><span style='color:#800080; '>;</span>

    Document pdfDocument <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>try</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#800000; font-weight:bold; '>string</span> htmlWithLinksAndAttachMarkers <span style='color:#808030; '>=</span> htmlStringTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>string</span> baseUrl <span style='color:#808030; '>=</span> baseUrlTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Convert a HTML string with markers for file links and attachments to a PDF document object</span>
        pdfDocument <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#808030; '>.</span>ConvertHtmlToPdfDocumentObject<span style='color:#808030; '>(</span>htmlWithLinksAndAttachMarkers<span style='color:#808030; '>,</span> baseUrl<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Display the attachments panel when the PDF document is opened in a PDF viewer</span>
        pdfDocument<span style='color:#808030; '>.</span>ViewerPreferences<span style='color:#808030; '>.</span>PageMode <span style='color:#808030; '>=</span> ViewerPageMode<span style='color:#808030; '>.</span>UseAttachments<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create File Attachments</span>

        <span style='color:#696969; '>// Create an attachment from a file without icon</span>
        <span style='color:#800000; font-weight:bold; '>string</span> fileAttachmentPath <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/Attach_Files/Attachment_File.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        pdfDocument<span style='color:#808030; '>.</span>AddFileAttachment<span style='color:#808030; '>(</span>fileAttachmentPath<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from File</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create an attachment from a stream without icon</span>
        <span style='color:#800000; font-weight:bold; '>string</span> fileStreamAttachmentPath <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/Attach_Files/Attachment_Stream.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream attachmentStream <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream<span style='color:#808030; '>(</span>fileStreamAttachmentPath<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileMode<span style='color:#808030; '>.</span>Open<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileAccess<span style='color:#808030; '>.</span>Read<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        pdfDocument<span style='color:#808030; '>.</span>AddFileAttachment<span style='color:#808030; '>(</span>attachmentStream<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment_Stream.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from Stream</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create an attachment from file with paperclip icon in PDF</span>
        HtmlElementMapping attachFromFileIconMapping <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#808030; '>.</span>HtmlElementsMappingOptions<span style='color:#808030; '>.</span>HtmlElementsMappingResult<span style='color:#808030; '>.</span>GetElementByMappingId<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attach_from_file_icon</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>attachFromFileIconMapping <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
        <span style='color:#800080; '>{</span>
            PdfPage attachFromFilePage <span style='color:#808030; '>=</span> attachFromFileIconMapping<span style='color:#808030; '>.</span>PdfRectangles<span style='color:#808030; '>[</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>]</span><span style='color:#808030; '>.</span>PdfPage<span style='color:#800080; '>;</span>
            RectangleF attachFromFileIconRectangle <span style='color:#808030; '>=</span> attachFromFileIconMapping<span style='color:#808030; '>.</span>PdfRectangles<span style='color:#808030; '>[</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>]</span><span style='color:#808030; '>.</span>Rectangle<span style='color:#800080; '>;</span>

            <span style='color:#800000; font-weight:bold; '>string</span> fileAttachmentWithIconPath <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/Attach_Files/Attachment_File_Icon.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

            <span style='color:#696969; '>// Create the attachment from file</span>
            FileAttachmentElement attachFromFileElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> FileAttachmentElement<span style='color:#808030; '>(</span>attachFromFileIconRectangle<span style='color:#808030; '>,</span> fileAttachmentWithIconPath<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
            attachFromFileElement<span style='color:#808030; '>.</span>IconType <span style='color:#808030; '>=</span> FileAttachmentIcon<span style='color:#808030; '>.</span>Paperclip<span style='color:#800080; '>;</span>
            attachFromFileElement<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from File with Paperclip Icon</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
            attachFromFileElement<span style='color:#808030; '>.</span>IconColor <span style='color:#808030; '>=</span> Color<span style='color:#808030; '>.</span>Blue<span style='color:#800080; '>;</span>
            attachFromFilePage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>attachFromFileElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800080; '>}</span>

        <span style='color:#696969; '>// Create an attachment from stream with pushpin icon in PDF</span>
        System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream attachmentStreamWithIcon <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#800080; '>;</span>
        HtmlElementMapping attachFromStreamIconMapping <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#808030; '>.</span>HtmlElementsMappingOptions<span style='color:#808030; '>.</span>HtmlElementsMappingResult<span style='color:#808030; '>.</span>GetElementByMappingId<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attach_from_stream_icon</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>attachFromStreamIconMapping <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
        <span style='color:#800080; '>{</span>
            PdfPage attachFromStreamPage <span style='color:#808030; '>=</span> attachFromStreamIconMapping<span style='color:#808030; '>.</span>PdfRectangles<span style='color:#808030; '>[</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>]</span><span style='color:#808030; '>.</span>PdfPage<span style='color:#800080; '>;</span>
            RectangleF attachFromStreamIconRectangle <span style='color:#808030; '>=</span> attachFromStreamIconMapping<span style='color:#808030; '>.</span>PdfRectangles<span style='color:#808030; '>[</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>]</span><span style='color:#808030; '>.</span>Rectangle<span style='color:#800080; '>;</span>

            <span style='color:#800000; font-weight:bold; '>string</span> fileStreamAttachmentWithIconPath <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/Attach_Files/Attachment_Stream_Icon.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

            attachmentStreamWithIcon <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream<span style='color:#808030; '>(</span>fileStreamAttachmentWithIconPath<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileMode<span style='color:#808030; '>.</span>Open<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileAccess<span style='color:#808030; '>.</span>Read<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

            <span style='color:#696969; '>// Create the attachment from stream</span>
            FileAttachmentElement attachFromStreamElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> FileAttachmentElement<span style='color:#808030; '>(</span>attachFromStreamIconRectangle<span style='color:#808030; '>,</span> attachmentStreamWithIcon<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment_Stream_Icon.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
            attachFromStreamElement<span style='color:#808030; '>.</span>IconType <span style='color:#808030; '>=</span> FileAttachmentIcon<span style='color:#808030; '>.</span>PushPin<span style='color:#800080; '>;</span>
            attachFromStreamElement<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from Stream with Pushpin Icon</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
            attachFromStreamElement<span style='color:#808030; '>.</span>IconColor <span style='color:#808030; '>=</span> Color<span style='color:#808030; '>.</span>Green<span style='color:#800080; '>;</span>
            attachFromStreamPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>attachFromStreamElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800080; '>}</span>

        <span style='color:#696969; '>// Save the PDF document in a memory buffer</span>
        <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Save<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Send the PDF as response to browser</span>

        <span style='color:#696969; '>// Set response content type</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>application/pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Instruct the browser to open the PDF file as an attachment or inline</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename=File_Links_and_Attachments.pdf; size={0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
        Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
        Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>finally</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Close the PDF document</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>pdfDocument <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            pdfDocument<span style='color:#808030; '>.</span>Close<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
</span>